package school.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import school.entity.Student;
import util.DBUtil;

public class MySqlStudentDaoImpl extends BaseDao implements StudentDao {

	// 新生注册
	public void addStudent(Student s) {
		String sql = "insert into student(sname,sex,telephone,qq,wechat,gradeid,enterdate,fromcity,birthday,address,pass)values(?,?,?,?,?,?,?,?,?,?,?)";
		Connection con = super.getConnection();
		PreparedStatement ps = null;

		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, s.getSname());
			ps.setString(2, s.getSex());
			ps.setString(3, s.getTelephone());
			ps.setString(4, s.getQq());
			ps.setString(5, s.getWechat());
			ps.setString(6, s.getGradeid());
			ps.setString(7, s.getEnterdate());
			ps.setString(8, s.getFormcity());
			ps.setString(9, s.getBirthday());
			ps.setString(10, s.getAddress());
			ps.setString(11, s.getPass());
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			super.close(con, ps, null);
		}

	}

	// 更改学生信息
	public void update(Student s) {
		String sql = "update student set pass=?";
		Connection con = super.getConnection();
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, s.getPass());
			// System.out.println(s.getPass());
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.close(con, ps, null);
		}
	}

	// 删除学生
	public void del(Student s) {
		String sql = "delete from student where stuno=?";
		// 调用父类BaseDao中的executeUpdate()方法执行删除操作
		super.executeUpdate(sql, s.getStuno());

	}

	// 根据学号查找单个学生信息
	public Student get(int id) {
		String sql = "select * from student where stuno=?";
		Connection con = super.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Student s = null;
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				// 如果找到了该学生,给学生赋值
				s = new Student();
				s.setAddress(rs.getString("address"));
				s.setBirthday(rs.getString("birthday"));
				s.setEnterdate(rs.getString("enterdate"));
				s.setFormcity(rs.getString("fromcity"));
				s.setGradeid(rs.getString("gradeid"));
				s.setPass(rs.getString("pass"));
				s.setQq(rs.getString("qq"));
				s.setSex(rs.getString("sex"));
				s.setSname(rs.getString("sname"));
				s.setStuno(rs.getInt("stuno"));
				s.setTelephone(rs.getString("telephone"));
				s.setWechat(rs.getString("wechat"));
			}
			return s;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		} finally {
			super.close(con, ps, rs);
		}

	}

	// 根据用户名和密码查找学生信息
	public Student login(int stuno, String pass) {
		String sql = "select * from student where stuno=? and pass=?";
		Connection con = super.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Student s = null;
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, stuno);
			ps.setString(2, pass);
			rs = ps.executeQuery();
			if (rs.next()) {
				// 如果用户名和密码正确,给学生赋值
				s = new Student();
				s.setAddress(rs.getString("address"));
				s.setBirthday(rs.getString("birthday"));
				s.setEnterdate(rs.getString("enterdate"));
				s.setFormcity(rs.getString("fromcity"));
				s.setGradeid(rs.getString("gradeid"));
				s.setPass(rs.getString("pass"));
				s.setQq(rs.getString("qq"));
				s.setSex(rs.getString("sex"));
				s.setSname(rs.getString("sname"));
				s.setStuno(rs.getInt("stuno"));
				s.setTelephone(rs.getString("telephone"));
				s.setWechat(rs.getString("wechat"));
			}
			return s;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}finally {
			super.close(con, ps, null);
		}

	}

	// 根据 页号，实现分页查询，返回数据的功能
	public List<Student> query(int pageNo, int pageSize) {
		List<Student> stulist = new ArrayList<Student>();
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Student s = null;
		try {
			String sql = "select * from student limit ?,?";
			ps = con.prepareStatement(sql);
			ps.setInt(1, (pageNo - 1) * pageSize);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				s = new Student();
				s.setAddress(rs.getString("address"));
				s.setBirthday(rs.getString("birthday"));
				s.setEnterdate(rs.getString("enterdate"));
				s.setFormcity(rs.getString("fromcity"));
				s.setGradeid(rs.getString("gradeid"));
				s.setPass(rs.getString("pass"));
				s.setQq(rs.getString("qq"));
				s.setSex(rs.getString("sex"));
				s.setSname(rs.getString("sname"));
				s.setStuno(rs.getInt("stuno"));
				s.setTelephone(rs.getString("telephone"));
				s.setWechat(rs.getString("wechat"));
				stulist.add(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.close(con, ps, null);
		}
		return stulist;
	}

	// 考生成绩添加
	public void addScore(int id, double score, int stuno, int subjectid) {
		String sql = "insert into examresult(id,score,st_id,sb_id)values(?,?,?,?)";
		Connection con = getConnection();
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			ps.setDouble(2, score);
			ps.setInt(3, stuno);
			ps.setInt(4, subjectid);
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}finally {
			super.close(con, ps, null);
		}

	}

}
